package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据实现层，和数据库打交道
 */
public class CompanyImpl implements CompanyDao {
    @Override
    public int add(CompanyModel companyModel) {

//        1.封装sql语句
        String sql = "insert into company (name,logo,phone,create_time,update_time ,content)" +
                " values('" + companyModel.getName() + "'," +
                "'" + companyModel.getLogo() + "'," +
                "'" + companyModel.getPhone() + "'," +
                "'" + companyModel.getCreateTime() + "'," +
                "'" + companyModel.getUpdateTime() + "'," +
                "'" + companyModel.getCompanyIntroduce() + "')";
//        2.调用JDBC链接

//        3.返回结果集
        return JDBC.excuteUpdate(sql);
    }

    @Override
    public CompanyModel getCompanyModel() {
        String sql = "select * from company order by id desc limit 1";
        ResultSet resultSet = JDBC.getResultSet(sql);
        CompanyModel model = null;
        try {
            while (resultSet.next()) {
                model = new CompanyModel();
                model.setId(resultSet.getInt("id"));
                model.setLogo(resultSet.getString("logo"));
                model.setAd(resultSet.getString("ad"));
                model.setNet(resultSet.getString("net"));
                model.setEmail(resultSet.getString("email"));
                model.setLinePhone(resultSet.getString("line_phone"));
                model.setVersion(resultSet.getString("version"));
                model.setPhone(resultSet.getString("phone"));
                model.setName(resultSet.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            JDBC.close();
        }
        return model;
    }

    @Override
    public List<CompanyModel> findAll() {
        String sql = "select * from company";
        ResultSet resultSet = JDBC.getResultSet(sql);
        List<CompanyModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                CompanyModel model = new CompanyModel();
                model.setId(resultSet.getInt("id"));
                model.setLogo(resultSet.getString("logo"));
                model.setAd(resultSet.getString("ad"));
                model.setNet(resultSet.getString("net"));
                model.setEmail(resultSet.getString("email"));
                model.setLinePhone(resultSet.getString("line_phone"));
                model.setVersion(resultSet.getString("version"));
                model.setPhone(resultSet.getString("phone"));
                model.setName(resultSet.getString("name"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public CompanyModel findById(int id) {
        String sql = "select * from company where id=" + id;
        ResultSet resultSet = JDBC.getResultSet(sql);
        CompanyModel model = null;
        try {
            while (resultSet.next()) {
                model = new CompanyModel();
                model.setId(resultSet.getInt("id"));
                model.setLogo(resultSet.getString("logo"));
                model.setAd(resultSet.getString("ad"));
                model.setNet(resultSet.getString("net"));
                model.setEmail(resultSet.getString("email"));
                model.setLinePhone(resultSet.getString("line_phone"));
                model.setVersion(resultSet.getString("version"));
                model.setPhone(resultSet.getString("phone"));
                model.setName(resultSet.getString("name"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return model;
    }

    @Override
    public int update(CompanyModel companyModel) {
        String sql = "update company set " +
                "name='" + companyModel.getName() + "'," +
                "logo='" + companyModel.getLogo() + "'," +
                "phone='" + companyModel.getPhone() + "'," +
                "content='" + companyModel.getCompanyIntroduce() + "' " +
                "where id=" + companyModel.getId();

        return JDBC.excuteUpdate(sql);
    }
}
